<?php

namespace App\Repositories\PayAndConsume;


use App\Models\Enum\ActionDef;
use App\Models\PayAndConsume\Hotspot;
use App\Repositories\BaseRepository;

class HotspotRepository extends BaseRepository
{
    public function __construct(Hotspot $model)
    {
        $this->model = $model;
    }

    public function create($data)
    {
        $hotspot = new Hotspot($data);
        return $hotspot->save();
    }

    //得到进入游戏链接
    public function getEnterUrl($request)
    {
        $gameID = session('game_id');

        $query = [
            [
                '$match' =>
                    [
                        'op_time' => [
                            '$gte' => $request['start_time'] . ' 00:00:00',
                            '$lte' => $request['end_time'] . ' 23:59:59'
                        ],
                        'game_id' =>
                            [
                                '$eq' => $gameID
                            ],
                        'action_id' =>
                            [
                                '$eq' => ActionDef::ENTER_URL
                            ],
                    ]
            ],
            [
                '$group' =>
                    [
                        '_id' =>
                            [
                                'channel_id' => '$channel_id',
                                'device_id' => '$device_id'
                            ],
                    ]
            ],
            [
                '$group' =>
                    [
                        '_id' =>
                            [
                                'channel_id' => '$_id.channel_id',
                            ],
                        'device_sum' => [
                            '$sum' => 1
                        ]
                    ]
            ],
            [
                '$project' =>
                    [
                        '_id' => 0,
                        'channel_id' => '$_id.channel_id',
                        'device_sum' => 1,
                    ]
            ]
        ];

        $collection = Hotspot::raw(function ($collection) use ($query) {
            return $collection->aggregate($query);
        });

        return $collection;
    }

    //获取进入游戏的Action
    public function getActionUid($request)
    {
        $gameID = session('game_id');

        $query = [
            [
                '$match' =>
                    [
                        'op_time' => [
                            '$gte' => $request['start_time'] . ' 00:00:00',
                            '$lte' => $request['end_time'] . ' 23:59:59'
                        ],
                        'game_id' =>
                            [
                                '$eq' => $gameID
                            ],
                        'action_id' =>
                            [
                                '$ne' => ActionDef::ENTER_URL
                            ],
                    ]
            ],
            [
                '$group' =>
                    [
                        '_id' =>
                            [
                                'channel_id' => '$channel_id',
                                'action_id' => '$action_id',
                                'uid' => '$uid'
                            ],
                    ]
            ],
            [
                '$group' =>
                    [
                        '_id' =>
                            [
                                'channel_id' => '$_id.channel_id',
                                'action_id' => '$_id.action_id',
                            ],
                        'uid_sum' => [
                            '$sum' => 1
                        ]
                    ]
            ],
            [
                '$project' =>
                    [
                        '_id' => 0,
                        'channel_id' => '$_id.channel_id',
                        'action_id' => '$_id.action_id',
                        'action_name' => 1,
                        'uid_sum' => 1,
                    ]
            ]
        ];

        $collection = Hotspot::raw(function ($collection) use ($query) {
            return $collection->aggregate($query);
        });

        return $collection;
    }
}